use "PNAS UKR survey replication data.dta"
*Data prep
*Note - all the variables are already included in data file 

*Generate standardized versions of EU support vars (ranging from 0-1)
*gen eu12_st=(eu_support_12+1)/2
*gen eu15_st=(eu_support_15+1)/2

*gen eu_chg_st1215=eu15_st-eu12_st

*gen eu15_avmgeo2_st= eu15_avmgeo2/2
*gen eu_chg_avmgeo2=eu15_avmgeo2_st-eu12_st

*gen eu15_avmbase_st= eu15_avmbase/2
*gen eu_chg_avmbase=eu15_avmbase_st-eu12_st

*gen eu15_chg_predgeo=eu15_pred_geog_st-eu12_st
*gen eu15_chg_predbase=eu15_pred_base-eu12_st

*Generate modified latitude and longitude for coordinates regression (by subtracting median latitude and longitude)
*summ latid longit if eu15_st~=., det
*gen latid_dm=latid-49.40568
*gen longit_dm=longit-30.85874

**Generate local shares of EU support for Figure 2
*Local shares 2012
*gen pro_eu12=0 if eu_support_12~=.
*recode pro_eu12 (0=1) if eu_support_12==1
*gen anti_eu12=0 if eu_support_12~=.
*recode anti_eu12 (0=1) if eu_support_12==-1

*sort loc_name1
*egen loc_pro_eu12_ct=sum(pro_eu12), by(loc_name1)
*egen loc_ct=count(pro_eu12), by(loc_name1)
*gen loc_pro_eu12_prop=loc_pro_eu12_ct/loc_ct
*drop loc_pro_eu12_ct loc_ct

*sort loc_name1
*egen loc_anti_eu12_ct=sum(anti_eu12), by(loc_name1)
*egen loc_ct=count(anti_eu12), by(loc_name1)
*gen loc_anti_eu12_prop=loc_anti_eu12_ct/loc_ct
*drop loc_anti_eu12_ct loc_ct

*gen loc_eu12_propdiff=loc_pro_eu12_prop-loc_anti_eu12_prop
*gen loc_eu12_propdiff_abs=abs(loc_eu12_propdiff)

*gen loc_eu12_anti_50plus=0 if loc_eu12_propdiff~=.
*recode loc_eu12_anti_50plus (0=1) if loc_eu12_propdiff<=-.5

*gen loc_eu12_pro_50plus=0 if loc_eu12_propdiff~=.
*recode loc_eu12_pro_50plus (0=1) if loc_eu12_propdiff>=.5

*Local shares 2015
*gen pro_eu15=0 if eu_support_15~=.
*recode pro_eu15 (0=1) if eu_support_15==1
*gen anti_eu15=0 if eu_support_15~=.
*recode anti_eu15 (0=1) if eu_support_15==-1

*sort loc_name1
*egen loc_pro_eu15_ct=sum(pro_eu15), by(loc_name1)
*egen loc_ct=count(pro_eu15), by(loc_name1)
*gen loc_pro_eu15_prop=loc_pro_eu15_ct/loc_ct
*drop loc_pro_eu15_ct loc_ct

*sort loc_name1
*egen loc_anti_eu15_ct=sum(anti_eu15), by(loc_name1)
*egen loc_ct=count(anti_eu15), by(loc_name1)
*gen loc_anti_eu15_prop=loc_anti_eu15_ct/loc_ct
*drop loc_anti_eu15_ct loc_ct

*gen loc_eu15_propdiff=loc_pro_eu15_prop-loc_anti_eu15_prop
*gen loc_eu15_propdiff_abs=abs(loc_eu15_propdiff)

*gen loc_eu15_anti_50plus=0 if loc_eu15_propdiff~=.
*recode loc_eu15_anti_50plus (0=1) if loc_eu15_propdiff<=-.5

*gen loc_eu15_pro_50plus=0 if loc_eu15_propdiff~=.
*recode loc_eu15_pro_50plus (0=1) if loc_eu15_propdiff>=.5

*Generate West&Center region indicators
*gen region_westctr=0 if oblast1~=.
*recode region_westctr (0=1) if oblast1==2|oblast1==3|oblast1==6|oblast1==7|oblast1==9|oblast1==10|oblast1==11|oblast1==12|oblast1==14|oblast1==17|oblast1==18|oblast1==19|oblast1==20|oblast1==23|oblast1==24|oblast1==25|oblast1==26

*Data prep for locality proportions based on AVM for Figure 4
*base AVM
*gen pro_eu15_avmbase=0 if eu15_avmbase_st~=.
*recode pro_eu15_avmbase (0=1) if eu15_avmbase_st==1

*gen anti_eu15_avmbase=0 if eu15_avmbase_st~=.
*recode anti_eu15_avmbase (0=1) if eu15_avmbase_st==0

*sort loc_name1
*egen loc_pro_eu15_avmbase_ct=sum(pro_eu15_avmbase), by(loc_name1)
*egen loc_ct=count(pro_eu15_avmbase), by(loc_name1)
*gen loc_pro_eu15_avmbase_prop=loc_pro_eu15_avmbase_ct/loc_ct
*drop loc_pro_eu15_avmbase_ct loc_ct

*sort loc_name1
*egen loc_anti_eu15_avmbase_ct=sum(anti_eu15_avmbase), by(loc_name1)
*egen loc_ct=count(anti_eu15_avmbase), by(loc_name1)
*gen loc_anti_eu15_avmbase_prop=loc_anti_eu15_avmbase_ct/loc_ct
*drop loc_anti_eu15_avmbase_ct loc_ct

*gen loc_eu15_avmbase_propdiff=loc_pro_eu15_avmbase_prop-loc_anti_eu15_avmbase_prop

*gen loc_eu15_avmbase_anti_50plus=0 if loc_eu15_avmbase_propdiff~=.
*recode loc_eu15_avmbase_anti_50plus (0=1) if loc_eu15_avmbase_propdiff<=-.5
*gen loc_eu15_avmbase_pro_50plus=0 if loc_eu15_avmbase_propdiff~=.
*recode loc_eu15_avmbase_pro_50plus (0=1) if loc_eu15_avmbase_propdiff>=.5

*Geographic AVM
*gen pro_eu15_avmgeo2=0 if eu15_avmgeo2_st~=.
*recode pro_eu15_avmgeo2 (0=1) if eu15_avmgeo2_st==1

*gen anti_eu15_avmgeo2=0 if eu15_avmgeo2_st~=.
*recode anti_eu15_avmgeo2 (0=1) if eu15_avmgeo2_st==0

*sort loc_name1
*egen loc_pro_eu15_avmgeo2_ct=sum(pro_eu15_avmgeo2), by(loc_name1)
*egen loc_ct=count(pro_eu15_avmgeo2), by(loc_name1)
*gen loc_pro_eu15_avmgeo2_prop=loc_pro_eu15_avmgeo2_ct/loc_ct
*drop loc_pro_eu15_avmgeo2_ct loc_ct

*sort loc_name1
*egen loc_anti_eu15_avmgeo2_ct=sum(anti_eu15_avmgeo2), by(loc_name1)
*egen loc_ct=count(anti_eu15_avmgeo2), by(loc_name1)
*gen loc_anti_eu15_avmgeo2_prop=loc_anti_eu15_avmgeo2_ct/loc_ct
*drop loc_anti_eu15_avmgeo2_ct loc_ct

*gen loc_eu15_avmgeo2_propdiff=loc_pro_eu15_avmgeo2_prop-loc_anti_eu15_avmgeo2_prop

*gen loc_eu15_avmgeo2_anti_50plus=0 if loc_eu15_avmgeo2_propdiff~=.
*recode loc_eu15_avmgeo2_anti_50plus (0=1) if loc_eu15_avmgeo2_propdiff<=-.5
*gen loc_eu15_avmgeo2_pro_50plus=0 if loc_eu15_avmgeo2_propdiff~=.
*recode loc_eu15_avmgeo2_pro_50plus (0=1) if loc_eu15_avmgeo2_propdiff>=.5




*Fig.1 Maps - see separate maps DB and do file

*Fig.2 - Shares of respondents in strongly pro- and anti-EU towns by region (based on raw data) - actual graphs made in Excel
*Summary stats used in Figure 2
summ loc_eu12_anti_50plus loc_eu12_pro_50plus loc_eu15_anti_50plus loc_eu15_pro_50plus if loc_eu15_pro_50plus~=.&region_westctr==0
summ loc_eu12_anti_50plus loc_eu12_pro_50plus loc_eu15_anti_50plus loc_eu15_pro_50plus if loc_eu15_pro_50plus~=.&region_westctr==1

*Data for Fig.3 (actual graphs made in Excel) and regressions for Table S1
*Generate coordinates with noise to allow spregress to run
*gen latid_noise=latid+rnormal(0,.05) 
*gen longit_noise=longit+rnormal(0,.05) 
spset, clear
spset nr_2012
spset, modify coord(latid_noise longit_noise)
spmatrix create idistance UKRgeowt, normalize(row) replace
spmatrix save UKRgeowt using  "UKRgeowt", replace

spmatrix use UKRgeowt using  "UKRgeowt", replace
spregress eu15_st c.eu12_st##c.discuss_freq1, ml force 
predict eu15pred_disc_nogeog if e(sample)==1, xb
outreg2 using "tab1_georeg.doc", replace append label dec(3) word symbol(**,*,#) nocons addstat(Pseudo R2, e(r2_p)) drop( ) 

spregress eu15_st c.eu12_st##c.discuss_freq1, ml ivarlag(UKRgeowt:eu12_st) force 
predict eu15pred_disc_geog if e(sample)==1, xb
outreg2 using "tab1_georeg.doc", append label dec(3) word symbol(**,*,#) nocons addstat(Pseudo R2, e(r2_p)) drop( ) 

spregress eu15_st c.eu12_st##c.discuss_freq1 i.region, ml ivarlag(UKRgeowt:eu12_st) force 
outreg2 using "tab1_georeg.doc", append label dec(3) word symbol(**,*,#) nocons addstat(Pseudo R2, e(r2_p)) drop( ) 

spregress eu15_st c.eu12_st##c.discuss_freq1 border_rus15 i.homelang_12 ethn_rus12 age1 female1 afford_ind1  voc_educ1 sec_educ1 hi_educ1 i.locsize1_3cat, ml ivarlag(UKRgeowt:eu12_st) force 
outreg2 using "tab1_georeg.doc", append label dec(3) word symbol(**,*,#) nocons addstat(Pseudo R2, e(r2_p)) drop( ) 

spregress eu15_st c.eu12_st##c.discuss_freq1 border_rus15 i.homelang_12 ethn_rus12 age1 female1 afford_ind1  voc_educ1 sec_educ1 hi_educ1 i.locsize1_3cat, ml ivarlag(UKRgeowt:eu12_st ethn_rus12 i.homelang_12) force 
outreg2 using "tab1_georeg.doc", append label dec(3) word symbol(**,*,#) nocons addstat(Pseudo R2, e(r2_p)) drop( ) 

*Correlations for Figure 3 - actual graph in Excel
pwcorr eu15_st eu15pred_disc_nogeog eu15pred_disc_geog 
pwcorr eu15_st eu15_avmbase eu15_avmgeo 


*Figure 4 - proportions of highly uniform towns based on different models for Figure 4 - actual graph in Excel
summ loc_eu12_anti_50plus loc_eu12_pro_50plus loc_eu15_anti_50plus loc_eu15_pro_50plus loc_eu15_avmgeo2_anti_50plus loc_eu15_avmgeo2_pro_50plus loc_eu15_avmbase_anti_50plus loc_eu15_avmbase_pro_50plus if loc_eu15_pro_50plus~=., sep(0)


*Figure 5, Figure S3 & Table S2: Geographic differences in EU change - model comparison
reg eu_chg_st1215 eu12_st c.latid_dm##c.latid_dm c.longit_dm##c.longit_dm
test latid_dm latid_dm#latid_dm longit_dm longit_dm#c.longit_dm
outreg2 using "coord_reg", replace label dec(4) word symbol(**,*,#) nocons drop( ) 
margins, at(longit_dm=-7.337) saving(eu_lon1, replace)
margins, at(longit_dm=0) saving(eu_lon2, replace)  
margins, at(longit_dm=6.009) saving(eu_lon3, replace) 
margins, at(latid_dm=-2.5506) saving(eu_lat1, replace)
margins, at(latid_dm=0) saving(eu_lat2, replace)  
margins, at(latid_dm=1.6402) saving(eu_lat3, replace) 


reg eu_chg_avmgeo2 eu12_st c.latid_dm##c.latid_dm c.longit_dm##c.longit_dm if eu_chg_st1215~=.
test latid_dm latid_dm#latid_dm longit_dm longit_dm#c.longit_dm
outreg2 using "coord_reg", append label dec(4) word symbol(**,*,#) nocons drop( ) 
margins, at(longit_dm=-7.337) saving(eu_lon4, replace) 
margins, at(longit_dm=0) saving(eu_lon5, replace)
margins, at(longit_dm=6.009) saving(eu_lon6, replace)
margins, at(latid_dm=-2.5506) saving(eu_lat4, replace)
margins, at(latid_dm=0) saving(eu_lat5, replace)  
margins, at(latid_dm=1.6402) saving(eu_lat6, replace) 

reg eu_chg_avmbase eu12_st c.latid_dm##c.latid_dm c.longit_dm##c.longit_dm if eu_chg_st1215~=.
test latid_dm latid_dm#latid_dm longit_dm longit_dm#c.longit_dm
outreg2 using "coord_reg", append label dec(4) word symbol(**,*,#) nocons drop( ) 
margins, at(longit_dm=-7.337) saving(eu_lon7, replace)
margins, at(longit_dm=0) saving(eu_lon8, replace)  
margins, at(longit_dm=6.009) saving(eu_lon9, replace) 
margins, at(latid_dm=-2.5506) saving(eu_lat7, replace)
margins, at(latid_dm=0) saving(eu_lat8, replace)  
margins, at(latid_dm=1.6402) saving(eu_lat9, replace) 

reg eu15_chg_predgeo eu12_st c.latid_dm##c.latid_dm c.longit_dm##c.longit_dm if eu_chg_st1215~=.
test latid_dm latid_dm#latid_dm longit_dm longit_dm#c.longit_dm
outreg2 using "coord_reg", append label dec(4) word symbol(**,*,#) nocons drop( ) 
margins, at(longit_dm=-7.337) saving(eu_lon10, replace)
margins, at(longit_dm=0) saving(eu_lon11, replace)  
margins, at(longit_dm=6.009) saving(eu_lon12, replace) 
margins, at(latid_dm=-2.5506) saving(eu_lat10, replace)
margins, at(latid_dm=0) saving(eu_lat11, replace)  
margins, at(latid_dm=1.6402) saving(eu_lat12, replace) 

reg eu15_chg_predbase eu12_st c.latid_dm##c.latid_dm c.longit_dm##c.longit_dm if eu_chg_st1215~=.
test latid_dm latid_dm#latid_dm longit_dm longit_dm#c.longit_dm
outreg2 using "coord_reg", append label dec(4) word symbol(**,*,#) nocons drop( ) 
margins, at(longit_dm=-7.337) saving(eu_lon13, replace)
margins, at(longit_dm=0) saving(eu_lon14, replace)  
margins, at(longit_dm=6.009) saving(eu_lon15, replace) 
margins, at(latid_dm=-2.5506) saving(eu_lat13, replace)
margins, at(latid_dm=0) saving(eu_lat14, replace)  
margins, at(latid_dm=1.6402) saving(eu_lat15, replace) 

*Figure 5
combomarginsplot  eu_lon10 eu_lon11 eu_lon12 eu_lon0  eu_lon7 eu_lon8 eu_lon9 eu_lon0 eu_lon4 eu_lon5 eu_lon6 eu_lon0 eu_lon1 eu_lon2 eu_lon3 eu_lon0, ///
title("") labels( "West" "Center" "East"  "Spatial regression" "West"  "Center" "East"  "Baseline AVM" "West"  "Center" "East"  "Spatial AVM"  "West" "Center" "East" "Raw data") ///	
    recast(scatter) horiz xline(0, lpattern(shortdash)) yscale(range(0.5 16.5)) ///
	ytitle(" ")   xtitle("Predicted EU support change", size(medium)) scheme(s2mono) 
graph save Graph "C:\Users\gpop\Dropbox (Princeton)\PNAS Special Issue\stata files\fig5_euchg_coordEW.gph"

*Figure S3
combomarginsplot  eu_lat12 eu_lat11 eu_lat10 eu_lon0  eu_lat9 eu_lat8 eu_lat7 eu_lon0 eu_lat6 eu_lat5 eu_lat4 eu_lon0 eu_lat3 eu_lat2 eu_lat1 eu_lon0, ///
title("") labels( "North" "Center" "South"  "Spatial regression" "North"  "Center" "South"  "Baseline AVM" "North"  "Center" "South"  "Spatial AVM"  "North" "Center" "South" "Raw data") ///	
    recast(scatter) horiz xline(0, lpattern(shortdash)) yscale(range(0.5 16.5)) ///
	ytitle(" ")   xtitle("Predicted EU support change", size(medium)) scheme(s2mono) 
graph save Graph "C:\Users\gpop\Dropbox (Princeton)\PNAS Special Issue\stata files\figS3_euchg_coordNS.gph"

	

*Table S3 - spatial regressions for scope conditions
spregress eu15_st eu12_st i.region, ml ivarlag(UKRgeowt:eu12_st) force
outreg2 using "spregress_other", replace label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress cu15_st cu12_st , ml ivarlag(UKRgeowt:cu12_st) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress nato15_st nato12_st , ml ivarlag(UKRgeowt:nato12_st) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress statelang_rus15 statelang_rus12, ml ivarlag(UKRgeowt:statelang_rus12) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress state_econ_ind15 state_econ_ind12, ml ivarlag(UKRgeowt:state_econ_ind12) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress democ_ind15 democ_ind12_st, ml ivarlag(UKRgeowt:democ_ind12_st) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress gay_rights15_st gay_rights12_st, ml ivarlag(UKRgeowt:gay_rights12_st) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress russ_neigh_st15 russ_neigh_st12, ml ivarlag(UKRgeowt:russ_neigh_st12) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress homelang_15_st homelang_12_st, ml ivarlag(UKRgeowt:homelang_12_st) force 
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress ethn_rus15 ethn_rus12, ml ivarlag(UKRgeowt:ethn_rus12) force  
outreg2 using "spregress_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 

*Table S3b - spatial regressions for scope conditions - with macro-region dummies

spregress eu15_st eu12_st i.region, ml ivarlag(UKRgeowt:eu12_st) force
outreg2 using "spregress_other_region", replace label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress cu15_st cu12_st i.region, ml ivarlag(UKRgeowt:cu12_st) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress nato15_st nato12_st i.region, ml ivarlag(UKRgeowt:nato12_st) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress statelang_rus15 statelang_rus12 i.region, ml ivarlag(UKRgeowt:statelang_rus12) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress state_econ_ind15 state_econ_ind12 i.region, ml ivarlag(UKRgeowt:state_econ_ind12) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress democ_ind15 democ_ind12_st i.region, ml ivarlag(UKRgeowt:democ_ind12_st) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress gay_rights15_st gay_rights12_st i.region, ml ivarlag(UKRgeowt:gay_rights12_st) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress russ_neigh_st15 russ_neigh_st12 i.region, ml ivarlag(UKRgeowt:russ_neigh_st12) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress homelang_15_st homelang_12_st i.region, ml ivarlag(UKRgeowt:homelang_12_st) force 
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 

spregress ethn_rus15 ethn_rus12 i.region, ml ivarlag(UKRgeowt:ethn_rus12) force  
outreg2 using "spregress_other_region", append label dec(3) word symbol(**,*,#) nocons drop( ) 



*Table S4 - coordinates regressions for scope conditions
reg eu_chg_st1215 eu12_st latid_dm longit_dm
test latid_dm longit_dm
outreg2 using "coord_reg_other", replace label dec(3) word symbol(**,*,#) nocons drop( ) 
reg cu_chg_st1215 cu12_st latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg nato_chg_st1215 nato12_st latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg statelang_rus_chg1215 statelang_rus12 latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg state_econ_ind_chg1215 state_econ_ind12 latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg democ_ind_chg1215 democ_ind12_st latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg gay_rights_chg1215 gay_rights12_st latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg russ_neigh_chg1215 russ_neigh_st12 latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg homelang_chg1215 homelang_12_st latid_dm longit_dm 
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 
reg ethn_rus_chg1215 ethn_rus12 latid_dm longit_dm  
test latid_dm longit_dm
outreg2 using "coord_reg_other", append label dec(3) word symbol(**,*,#) nocons drop( ) 


*Table S5: Summary stats
summ eu12_st cu12_st nato12_st state_econ_ind12_st democ_ind12_st gay_rights12_st statelang_rus12 russ_neigh_st12 homelang_12_st ethn_rus12  ///
eu_chg_st1215 cu_chg_st1215 nato_chg_st1215 state_econ_ind_chg1215 democ_ind_chg1215 gay_rights_chg1215 statelang_rus_chg1215 russ_neigh_chg1215 homelang_chg1215 ethn_rus_chg1215 ///
discuss_freq1 border_rus15 age1 female1 afford_ind1  voc_educ1 sec_educ1 hi_educ1 locsize1_3cat if eu15_st~=., sep(0)




